package ability

import (
	"sjy/customcmd/src/entity"
)

var FuncManager = &AbstractFuncManager{
	objects: []AbstractFunc{},
}

type AbstractFuncManager struct {
	objects []AbstractFunc
}

func (r *AbstractFuncManager) Register(obj AbstractFunc) AbstractFunc {
	r.objects = append(r.objects, obj)
	return obj
}

func (r *AbstractFuncManager) FindFunc(className string) AbstractFunc {
	for _, obj := range r.objects {
		if obj.GetClassName() == className {
			return obj
		}
	}
	return nil
}

type AbstractFunc interface {
	Execute(obj *FuncEntity)
	GetClassName() string
	SetClassName(name string)
}

type FuncObjBase struct {
	ClassName string
}

func (r *FuncObjBase) SetClassName(str string) {
	r.ClassName = str
}

func (r *FuncObjBase) GetClassName() string {
	return r.ClassName
}

type FuncEntity struct {
	Paramenter *entity.Param
}

func Execute(obj *FuncEntity) {
	funcClass := FuncManager.FindFunc(obj.Paramenter.MainCmd.C())
	if funcClass == nil {
		panic("cmd not found")
	}
	funcClass.Execute(obj)
}
